﻿#include "SocketDialog.h"
#include "../../MirGraphics/MLibrary.h"
#include "../../MirSounds/SoundManager.h"
#include "../GameScene.h"

using namespace Client::MirControls;
using namespace Client::MirGraphics;
using namespace Client::MirSounds;

namespace Client::MirScenes::Dialogs
{

    SocketDialog::SocketDialog()
    {
        setIndex(20);
        setLibrary(Libraries::Prguse3);
        setMovable(true);
        setSort(true);
        Point tempVar(0, 0);
        setLocation(&tempVar);

        Grid = std::vector<MirItemCell*>(6 * 2);

        for (int x = 0; x < 6; x++)
        {
            for (int y = 0; y < 2; y++)
            {
                int idx = 6 * y + x;

                MirItemCell *tempVar2 = new MirItemCell();
                tempVar2->setItemSlot(idx);
                tempVar2->setGridType(MirGridType::Socket);
                tempVar2->setLibrary(Libraries::Items);
                tempVar2->setParent(this);
                Point tempVar3(x * 36 + 23 + x, y * 33 + 15 + y);
                tempVar2->setLocation(&tempVar3);
                Grid[idx] = tempVar2;

                delete tempVar2;
            }
        }

        CloseButton = new MirButton();
        CloseButton->setHoverIndex(361);
        CloseButton->setIndex(360);
        Point tempVar4(getSize()->Width - 23, 3);
        CloseButton->setLocation(&tempVar4);
        CloseButton->setLibrary(Libraries::Prguse2);
        CloseButton->setParent(this);
        CloseButton->setPressedIndex(362);
        CloseButton->setSound(SoundList::ButtonA);
        CloseButton->Click += [&] (o, e)
        {
            Hide();
        };
    }

    void SocketDialog::BindGrid()
    {
        int count = 0;

        if (GameScene::SelectedItem != nullptr)
        {
            count = GameScene::SelectedItem->Slots.size();
        }

        for (int x = 0; x < 6; x++)
        {
            for (int y = 0; y < 2; y++)
            {
                int idx = 6 * y + x;

                Grid[idx]->setVisible(idx < count);
            }
        }
    }

    MirItemCell *SocketDialog::GetCell(unsigned long long id)
    {
        for (int i = 0; i < Grid.size(); i++)
        {
            if (Grid[i]->getItem() == nullptr || Grid[i]->getItem().UniqueID != id)
            {
                continue;
            }
            return Grid[i];
        }
        return nullptr;
    }

    void SocketDialog::Hide()
    {
        GameScene::SelectedItem = nullptr;
        MirImageControl::Hide();
    }

    void SocketDialog::Show(MirGridType grid, UserItem *item)
    {
        if (item->Slots.empty())
        {
            GameScene::SelectedItem = nullptr;
            setVisible(false);
            return;
        }

        GameScene::SelectedItem = item;

        setIndex(20 + (GameScene::SelectedItem->Slots.size() - 1));

        BindGrid();

        Point tempVar(getSize()->Width - 23, 3);
        CloseButton->setLocation(&tempVar);

        switch (grid)
        {
            case MirGridType::Inventory:
                Point tempVar2(GameScene::Scene->InventoryDialog->getLocation()->X + ((GameScene::Scene->InventoryDialog->getSize()->Width - getSize()->Width) / 2), GameScene::Scene->InventoryDialog->getLocation()->Y + GameScene::Scene->InventoryDialog->getSize()->Height + 5);
                setLocation(&tempVar2);
                break;
            case MirGridType::Equipment:
                Point tempVar3(GameScene::Scene->CharacterDialog->getLocation()->X + ((GameScene::Scene->CharacterDialog->getSize()->Width - getSize()->Width) / 2), GameScene::Scene->CharacterDialog->getLocation()->Y + GameScene::Scene->CharacterDialog->getSize()->Height + 5);
                setLocation(&tempVar3);
                break;
        }

        setVisible(true);
    }
}
